package com.example.myvideoplayer.presenter;

import android.content.Context;
import android.os.Handler;
import android.view.Surface;
import android.view.SurfaceHolder;
import com.example.myvideoplayer.Bean.Video;
import com.example.myvideoplayer.Utils.Untils;
import com.example.myvideoplayer.module.VideoPlayer;
import java.util.ArrayList;
public class VideoPresenter {
    private ArrayList<Video> videoList;
    private static VideoPresenter videoPresenter;
    private VideoPlayer videoPlayer;
    private int position = 0;
    private VideoPresenter(){
       if(videoPlayer == null){
           videoPlayer = VideoPlayer.getInstance();
       }
    }
    public static VideoPresenter getInstance(){
        if(videoPresenter == null){
            videoPresenter = new VideoPresenter();
        }
        return videoPresenter;
    }
    public void setGLSurface(Surface glSurface) {
        videoPlayer.setGLSurface(glSurface);
    }
    public void setHandler(Handler handler){
        videoPlayer.setHandler(handler);
    }
    public void scanVideo(Context context){
        videoList = Untils.scanAllAudioFiles(context);
    }
    public ArrayList<Video> getVideoList(){
        return videoList;
    }
    public void play(){
        videoPlayer.setFinish(true);
        String url = videoList.get(position).getUrl();
        videoPlayer.play(url);
    }
    public void playOrPause(){
        videoPlayer.playOrPause();
    }
    public void stop(){
        videoPlayer.stop();
    }
    public int getDuration(){
        return videoPlayer.getDuration();
    }
    public void setSeekBarStatus(boolean status){
        videoPlayer.setSeekBarIsChanging(status);
    }
    public void seekTo(int position){
        videoPlayer.seekTo(position);
    }
    public void exit(){
        videoPlayer = null;
        videoPresenter = null;
    }
    public void setPosition(int position){
        this.position = position;
    }
    public void savePosition(){
        videoPlayer.savePosition();
    }
    public void next(){
        if(position< (videoList.size()-1)){
            position ++;
        } else {
            position = 0;
        }
        play();
    }
    public boolean isPlaying(){
        return videoPlayer.isPlaying();
    }
}
